// pages/user/user.js
import { Address } from './address-model.js';

const app = getApp()
const $address = new Address()

Page({

  /**
   * 页面的初始数据
   */
  data: {
      userInfo: {},
      dialogShow:false,
      threeArea:{
        provice:null,
        city: null,
        area: null
      },
      hasCity:true,
      hasArea:true,
      editAddress:null,
      editRegion:[],
      addressList:null,
      isShowPopup:false,
      tips:'',
      toggleWord:'新增',
      editId:'',
      loadDone:false,
      editStatusReset:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    let that = this
    let userInfo = wx.getStorageSync('userInfo')
    wx.showLoading({
        title: '加载中',
    })
    this.setData({
        userInfo: userInfo,
    })
    let provice = {
            "data": {
                "pid": "0"
            }
        }
    
      // 获取省一级 pid 0
    $address.getThree(provice,res=>{
        if(res.status){
            that.setData({
                'threeArea.provice': res.data
            })
        } else {
            wx.hideLoading()
            $address.showMyTip({ _this: this, title: "服务器错误" })
        }
    })
    // 获取地址列表
    $address.getUserAddress({
        data:{
            "userId": (this.data.userInfo.id).toString()
        }
    },res=>{
        let status = res.status;
        let data = res.data;
        if(status){
            that.setData({
                addressList: data,
                loadDone:true
            })
            wx.hideLoading()
        } else {
            wx.hideLoading()
            $address.showMyTip({ _this: this, title: "服务器错误" })
        }
    })
  },
  preventD(e){
      return;
  },
  // 设置默认地址
  setDefault(e){
    let id = e.currentTarget.dataset.id
    let that = this
    let params = {
        data: {
            "userId": "" + this.data.userInfo.id,
            "id": "" + id,
            "defaultOptions": "1"
        }
    }
    console.log(params)
    this.commonUpdate(params, '确认设置为默认地址吗？', '设置成功')
  },
//   编辑地址
  edit(e){
    let province, city, area;
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
        url: '/pages/editAddress/editAddress?id=' + id,
    })
    
  },
//   删除地址
  delete(e){
    let id = e.currentTarget.dataset.id
    let that = this
    let params = {
        data: {
            "userId": "" + this.data.userInfo.id,
            "id": "" + id,
            "status": "1"
        }
    }
    console.log(params)
    this.commonUpdate(params,'确定删除吗？','删除成功')
  },
  commonUpdate(params,content,title){
      var that = this
      wx.showModal({
          title: '提示',
          content: content,
          success: function (res) {
              if (res.confirm) {
                  $address.updateUserAddress(params, res2 => {
                      console.log(res2)
                      let status = res2.status
                      if (status) {
                          wx.showToast({
                              title: title,
                              icon: 'success',
                              duration: 2000,
                              success() {
                                  setTimeout(()=>{
                                    that.onLoad()
                                  },2000)
                              }
                          })
                      } else {
                          wx.hideLoading()
                          $address.showMyTip({ _this: this, title: "服务器错误" })
                      }
                  })
              } else if (res.cancel) {
                  console.log('用户点击取消')
              }
          }
      })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //this.onLoad()
    $address.getUserAddress({
        data: {
            "userId": (this.data.userInfo.id).toString()
        }
    }, res => {
        let status = res.status;
        let data = res.data;
        if (status) {
            this.setData({
                addressList: data,
            })
            wx.hideLoading()
            wx.stopPullDownRefresh()
        } else {
            wx.hideLoading()
            wx.stopPullDownRefresh()
            $address.showMyTip({ _this: this, title: "服务器错误" })
        }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },
})